package ch.zbw.pra.sysventory.presentation.main;

import ch.zbw.pra.sysventory.application.controller.UserController;
import ch.zbw.pra.sysventory.presentation.panels.ApplicationPanel;
import ch.zbw.pra.sysventory.presentation.panels.LoginPanel;

import com.vaadin.Application;
import com.vaadin.ui.AbstractLayout;
import com.vaadin.ui.Window;

/**
 * Main-Methode
 * 
 * @author Ralph Koster
 */

public class Main extends Application {

	private static final long serialVersionUID = -4534906507703860511L;
	private UserController userController;
	private Window main;
	private AbstractLayout currentPanel;
	private LoginPanel loginPanel;
	private ApplicationPanel applicationPanel;
	
	
	@Override
    public void init() {
        // Main window is the primary browser window
		userController = UserController.getInstance();
        main = new Window("Sysventory Hard- und Software Inventarisierung");
        main.setTheme("sysventory");
        loginPanel = new LoginPanel(this);
        currentPanel = loginPanel;
        currentPanel.setMargin(false);
        main.setStyleName("main-while-login");
        main.setSizeFull();
        main.addComponent(currentPanel);
        this.setMainWindow(main); 
	}
	
	public void showLoginPanel(){
		userController.setIsLoggedIn(false);
	    main.replaceComponent(currentPanel, loginPanel);
	    main.setStyleName("main-while-login");
	    currentPanel = loginPanel;
	}

	public void showApplicationPanel(){
		applicationPanel = new ApplicationPanel(this);
	    main.replaceComponent(currentPanel, applicationPanel);
	    main.setStyleName("main-after-login");
	    currentPanel = applicationPanel;
	}
	
	public boolean checkLogin(String username, String password) {
		boolean bool = userController.checkLogin(username, password);
		if(bool) {
			showApplicationPanel();
		}
		return bool;
	}
}
